﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using SystemModule;
using Nett;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;

namespace ConfigTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var line = string.Empty;
            var doc = new XmlDocument();
            var xmldec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(xmldec);
            var xmlele1 = doc.CreateElement("M2Server");
            doc.AppendChild(xmlele1);
            XmlElement xmlele2 = null;
            using (var sr = new System.IO.StreamReader(@"D:\MirServer\Mir200\!SetUp.txt", Encoding
                .Default))
            {
                var nullCount = 0;
                while (true)
                {
                    line = sr.ReadLine();
                    if (nullCount >= 10)
                    {
                        break;
                    }
                    if (string.IsNullOrEmpty(line))
                    {
                        nullCount++;
                        continue;
                    }
                    nullCount = 0;
                    if (line.StartsWith(";"))
                    {
                        continue;
                    }
                    if (line.StartsWith("[") && line.EndsWith("]"))
                    {
                        xmlele2 = doc.CreateElement(line.Replace("[", "").Replace("]", ""));
                        xmlele1.AppendChild(xmlele2);
                    }
                    else
                    {
                        var str = line.Split('=');
                        XmlElement name = doc.CreateElement(str[0].Trim());
                        if (string.IsNullOrEmpty(str[1].Trim()))
                        {
                            name.InnerText = " ";
                        }
                        else
                        {
                            name.InnerText = str[1].Trim().Replace("'", "");
                        }
                        xmlele2.AppendChild(name);
                    }
                }
            }
            doc.Save("Books.xml");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (var sr = new System.IO.StringReader("config.toml"))
            {
                var str = sr.ReadToEnd();

                var parsed = Toml.ReadString(str);

                var sss = parsed.Get<string>("str");
                MessageBox.Show(sss);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var line = string.Empty;
            var doc = new XmlDocument();
            var xmldec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(xmldec);
            var xmlele1 = doc.CreateElement("M2Server");
            doc.AppendChild(xmlele1);
            XmlElement xmlele2 = null;
            using (var sr = new System.IO.StreamReader(@"D:\MirServer\Mir200\Command.ini", Encoding
                .Default))
            {
                var nullCount = 0;
                while (true)
                {
                    line = sr.ReadLine();
                    if (nullCount >= 10)
                    {
                        break;
                    }
                    if (string.IsNullOrEmpty(line))
                    {
                        nullCount++;
                        continue;
                    }
                    nullCount = 0;
                    if (line.StartsWith(";"))
                    {
                        continue;
                    }
                    if (line.StartsWith("[") && line.EndsWith("]"))
                    {
                        xmlele2 = doc.CreateElement(line.Replace("[", "").Replace("]", ""));
                        xmlele1.AppendChild(xmlele2);
                    }
                    else
                    {
                        var str = line.Split('=');
                        XmlElement name = doc.CreateElement(str[0].Trim());
                        if (string.IsNullOrEmpty(str[1].Trim()))
                        {
                            name.InnerText = " ";
                        }
                        else
                        {
                            name.InnerText = str[1].Trim().Replace("'", "");
                        }
                        xmlele2.AppendChild(name);
                    }
                }
            }
            doc.Save("123.xml");
        }
    }
}